package com.android.RssReader.Screen;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

import com.android.RssReader.R;
import com.android.RssReader.RssReaderApplication;
import com.android.RssReader.Adapter.NewsItemAdapter;
import com.android.RssReader.Service.ScreenService;
import com.android.RssReader.Service.ServiceManager;


public class ScreenFavorites extends Screen{
	
    private final ScreenService screenService;
	
	public ScreenFavorites() {
		super(SCREEN_TYPE.SCREEN_FAVORITES_T, ScreenFavorites.class.getCanonicalName());
		screenService = ServiceManager.getScreenservice();
	}
	
	private ListView mListView;
	private NewsItemAdapter mAdapter;
	private Cursor c ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		RssReaderApplication.LogD(ScreenFavorites.class, "onCreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screen_favorites);
		mListView = (ListView) findViewById(R.id.favorites_news_list);
	    c = ServiceManager.getNewsservice().getMediaDB().queryCountLocalFavoritesNews();
		mAdapter = new NewsItemAdapter(this, c);
		mListView.setAdapter(mAdapter);
		startManagingCursor(c);
		mListView.setOnItemClickListener(mItemClickListener);
		mListView.setOnItemLongClickListener(mItemLongClickListener);
	}
	
	@Override
	protected void onResume() {
		RssReaderApplication.LogD(ScreenFavorites.class, "onResume");
		super.onResume();
	}

	@Override
	protected void onNewIntent(Intent intent) {
		RssReaderApplication.LogD(ScreenFavorites.class, "onNewIntent");
		super.onNewIntent(intent);
		c = ServiceManager.getNewsservice().getMediaDB().queryCountLocalFavoritesNews();
		mAdapter.changeCursor(c);
		mAdapter.notifyDataSetChanged();
	}
	
	private OnItemClickListener mItemClickListener = new OnItemClickListener(){

		@Override
		public void onItemClick(AdapterView<?> adapterView, View view, int position,
				long _id) {
			ScreenArgs args = (ScreenArgs) view.getTag();
			args.putExtra("item_id", _id);
			screenService.show(ScreenNewsContent.class, null , args, true);
		}
	};
	
	private OnItemLongClickListener mItemLongClickListener = new OnItemLongClickListener(){

		@Override
		public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
				int position, final long _id) {
			Toast.makeText(ScreenFavorites.this, "position:"+position, Toast.LENGTH_SHORT).show();
		    AlertDialog.Builder builder = new AlertDialog.Builder(ScreenFavorites.this); 
		    builder.setMessage(getString(R.string.dialog_delete_favorites)) 
		           .setCancelable(false) 
		           .setPositiveButton(R.string.alert_ok, new DialogInterface.OnClickListener() { 
		               @Override
					public void onClick(DialogInterface dialog, int id) { 
		            	   ServiceManager.getNewsservice().getMediaDB().deleteCountLocalFavorites(_id);
		            	   c.requery();
		           	       mAdapter.changeCursor(c);
		           		   mAdapter.notifyDataSetChanged();
		               } 
		           }) 
		           .setNegativeButton(R.string.alert_cancel, new DialogInterface.OnClickListener() { 
		               @Override
					public void onClick(DialogInterface dialog, int id) { 
		                    dialog.cancel(); 
		               } 
		           }); 
		    AlertDialog alert = builder.create(); 
		    alert.show();
			return false;
		}};
		
    	
	
}
